<template>
  <el-container class="mainContent">
    <el-row>
      <el-col class="title">添加家庭医生团队信息</el-col>
    </el-row>
    <el-form ref="form" :model="form" label-width="150px">
      <el-form-item label="团队名称：">
        <el-input v-model="form.groupName"></el-input>
      </el-form-item>
      <el-form-item label="团队介绍：">
        <el-input v-model="form.teamInfo"></el-input>
      </el-form-item>
      <el-form-item label="团队logo：">
        <el-image style="width: 200px; height: 200px" :src="url" fit="cover"></el-image>
      </el-form-item>
      <el-row>
        <el-col class="subtitle">家庭医生成员管理：</el-col>
      </el-row>
      <el-form-item label="医生成员列表">
        <el-select v-model="form.region" placeholder="请选择医生成员">
          <el-option
            :label="item.d_name"
            :value="item.d_id"
            v-for="(item,index) in list"
            :key="index"
          ></el-option>
        </el-select>
        <el-button @click.prevent="addMember()">添加</el-button>
      </el-form-item>
      <el-form-item>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="d_name" label="家庭医生姓名" width="140"></el-table-column>
          <el-table-column prop="manager" label="负责人" width="140">
            <template slot-scope="scope">
              <span :class="getMainDoctor(scope.row.manager)"></span>
            </template>
          </el-table-column>
          <el-table-column prop="d_sex" label="性别" width="140"></el-table-column>
          <el-table-column prop="d_sid" label="身份证" width="300"></el-table-column>
          <el-table-column prop="d_tel" label="手机号" width="200"></el-table-column>
          <el-table-column prop="d_join" label="职称" width="200"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">保存</el-button>
        <el-button @click="back">返回</el-button>
      </el-form-item>
    </el-form>
  </el-container>
</template>
<script>
export default {
  name: "AddTeamManagement",
  data() {
    return {
      form: {
        groupName: "",
        teamInfo: "",
        region: "",
      },
      list: [],
      tid: 0,
      tableData: [],
      baseUrl: "http://127.0.0.1:8001/",
      url: "",
    };
  },
  methods: {
    getDotorData() {
      // console.log(this.tid)
      this.axios({
        url: "/getAllDoctor",
        method: "get",
      })
        .then((res) => {
          // console.log(res);
          if (res.data.code === 200) {
            this.list = res.data.data;
            console.log(res);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getMainDoctor(manager) {
      return manager === 1 ? "el-icon-user-solid" : "el-icon-user";
    },
    addMember() {
      // console.log(this.form.region)
      this.$confirm("是否将此医生添加到成员列表, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // this.form.region.d_id
          //   console.log(this.list);
          //   console.log(this.form.region);
          let result = this.list.find((item) => {
            return this.form.region == item.d_id;
          });
          //   console.log(result);
          if (this.tableData.indexOf(result) >= 0) {
            this.$message({
              showClose: true,
              message: "该医生已添加，不能重复添加",
              type: "error",
            });
          } else {
            this.tableData.push(result);
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleDelete(index, row) {
      // console.log(index, row);
      this.$confirm("确定将此成员删除吗？, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let result = this.tableData.find((item) => {
            return row.d_id == item.d_id;
          });
          let index=this.tableData.indexOf(result)
          this.tableData.splice(index,1)
           this.$message({
          message: '删除成功',
          type: 'success'
        });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    onSubmit() {
      this.$confirm("确定添加此团队信息？, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
            this.form.teamItems=this.tableData
          this.axios({
            url: "/addTeamData",
            method: "post",
            data: this.form,
          })
            .then((res) => {
              if (res.data.code === 200) {
                this.$router.push("/home/team");
                console.log(res)
                this.$message({
                  message: "添加成功",
                  type: "success",
                });
              } else {
                this.$message.error("添加失败");
              }
            })
            .catch((err) => {
              this.$message.error("添加失败");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消添加",
          });
        });
    },
    back() {
      this.$router.go(-1);
    },
  },
  mounted() {
    this.getDotorData();
  },
};
</script>
<style lang="less" scoped>
.form-inline {
  width: 100%;
  height: 300px;
  text-align: left;
}
.mainContent {
  width: 100%;
  flex-direction: column;
  text-align: left;
}
.search-layer {
  width: 100%;
}
.pagination {
  margin-top: 50px;
  text-align: center;
}
.title {
  margin-bottom: 30px;
  font-size: 20px;
  font-weight: 600;
}
.subtitle {
  margin: 30px 0;
  font-size: 16px;
  font-weight: 600;
}
</style>